<template>
  <div>
    <el-button
      type="primary"
      icon="el-icon-edit"
      circle
      @click="putUser"
    ></el-button>

    <!-- #region Dialog -->
    <el-dialog
      title="修改用户"
      :visible.sync="isVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        ref="putFromRef"
        :model="putFrom"
        :rules="putFormRoles"
        label-width="80px"
        status-icon
      >
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="putFrom.userName" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="putFrom.email" clearable></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="putFrom.phone" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit('putFromRef')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- #endregion -->
  </div>
</template>

<script>
export default {
  props: {
    user: Object,
  },
  data() {
    const validateEmail = (rule, value, callback) => {
      if (/^\w+@\w+\.\w+$/.test(value)) {
        callback();
      } else {
        callback(new Error("无效邮箱"));
      }
    };
    var validatePhone = (rule, value, callback) => {
      if (/^1[3-9][0-9]{9}$/.test(value)) {
        callback();
      } else {
        callback(new Error("无效手机号"));
      }
    };
    return {
      isVisible: false,
      putFrom: { userName: "", email: "", phone: "" },
      putFormRoles: {
        email: [
          { required: true, message: "不能为空", trigger: "blur" },
          {
            min: 6,
            message: "长度在 不小于 6 个字符",
            trigger: "blur",
          },
          {
            max: 15,
            message: "长度在 不大于 15 个字符",
            trigger: "blur",
          },
          { validator: validateEmail, trigger: "blur" },
        ],
        phone: [
          { required: true, message: "不能为空", trigger: "blur" },
          { validator: validatePhone, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    putUser() {
      this.isVisible = true;
      this.putFrom = this.user;
    },
    handleClose() {
      this.isVisible = false;
    },
    onSubmit(formName) {
      console.log(this.$refs[formName]);
      this.$refs[formName].validate(async (valided) => {
        if (!valided) {
          return;
        }
        try {
          let response = await this.$myhttp.put(
            "User",
            this.putFrom.userName,
            this.putFrom
          );
          console.log(response);
          this.isVisible = false;
          this.$message.success("修改成功");
          this.$emit("putedUser", response.data);
        } catch (error) {
          console.error(error);
          this.$message.error("修改失败");
        }
      });
    },
  },
};
</script>

<style></style>
